package card_discount

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"bp/internal/util"
	"context"
	"fmt"
)

// GetCouponDistributionPtr 获取卡折扣
func (s *CardDiscountEntityServiceImp) GetCardDiscountPtr(ctx context.Context, q *sqlc.Queries, cardDiscountID int64) (cardDiscountPtr *sqlc.CardDiscount, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	cardDiscount, err := q.GetCardDiscount(ctx, cardDiscountID)
	if err != nil {
		return
	}
	slog.Infof("1 GetCardDiscountPtr ok")

	cardDiscountPtr = &cardDiscount
	return
}

// GetCardDiscountPtrByOutCardID 根据外部卡id获取卡折扣
func (s *CardDiscountEntityServiceImp) GetCardDiscountPtrByOutCardID(ctx context.Context, q *sqlc.Queries, outCardID int64) (cardDiscountPtr *sqlc.CardDiscount, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	cardDiscount, err := q.GetCardDiscountByOutCardID(ctx, outCardID)
	if err != nil {
		return
	}
	slog.Infof("1 GetCardDiscountPtrByOutCardID ok")

	cardDiscountPtr = &cardDiscount
	return
}

// ListCardDiscountPtrsParam 列出卡折扣参数
type ListCardDiscountPtrsParam struct {
	UseScenes []string
	Keyword   string
}

// ListCardDiscountPtrs 列出卡折扣
func (s *CardDiscountEntityServiceImp) ListCardDiscountPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCardDiscountPtrsParam) (total int64, cardDiscountPtrs []*sqlc.CardDiscount, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountCardDiscounts(ctx, sqlc.CountCardDiscountsParams{
		Ts:                cPtr.PagerPtr.Ts,
		BrandID:           cPtr.BrandID,
		FilterAllUseScene: len(ptr.UseScenes) == 0,
		UseScenes:         ptr.UseScenes,
		IsKeywordEmpty:    util.IsEmpty(ptr.Keyword),
		Keyword:           fmt.Sprintf("%%%s%%", ptr.Keyword),
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListCardDiscounts(ctx, sqlc.ListCardDiscountsParams{
		Ts:                cPtr.PagerPtr.Ts,
		BrandID:           cPtr.BrandID,
		FilterAllUseScene: len(ptr.UseScenes) == 0,
		UseScenes:         ptr.UseScenes,
		IsKeywordEmpty:    util.IsEmpty(ptr.Keyword),
		Keyword:           fmt.Sprintf("%%%s%%", ptr.Keyword),
		Offset:            cPtr.PagerPtr.Offset,
		Limit:             cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	slog.Infof("1 ListCardDiscountPtrs ok")

	cardDiscountPtrs = s.MapCardDiscountEntityToPtr(rows)
	return
}

// ListCardDiscountWithStatPtrsParam 列出卡折扣及其统计参数
type ListCardDiscountWithStatPtrsParam struct {
	UseScenes []string
	Keyword   string
}

// ListCardDiscountWithStatPtrs 列出卡折扣及其统计
func (s *CardDiscountEntityServiceImp) ListCardDiscountWithStatPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCardDiscountWithStatPtrsParam) (total int64, cardDiscountWithStatPtrs []*sqlc.ListCardDiscountWithStatsRow, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountCardDiscounts(ctx, sqlc.CountCardDiscountsParams{
		Ts:                cPtr.PagerPtr.Ts,
		BrandID:           cPtr.BrandID,
		FilterAllUseScene: len(ptr.UseScenes) == 0,
		UseScenes:         ptr.UseScenes,
		IsKeywordEmpty:    util.IsEmpty(ptr.Keyword),
		Keyword:           fmt.Sprintf("%%%s%%", ptr.Keyword),
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListCardDiscountWithStats(ctx, sqlc.ListCardDiscountWithStatsParams{
		Ts:                cPtr.PagerPtr.Ts,
		BrandID:           cPtr.BrandID,
		FilterAllUseScene: len(ptr.UseScenes) == 0,
		UseScenes:         ptr.UseScenes,
		IsKeywordEmpty:    util.IsEmpty(ptr.Keyword),
		Keyword:           fmt.Sprintf("%%%s%%", ptr.Keyword),
		Offset:            cPtr.PagerPtr.Offset,
		Limit:             cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	slog.Infof("1 ListCardDiscountWithStatPtrs ok")

	cardDiscountWithStatPtrs = s.MapCardDiscountWithStatEntityToPtr(rows)
	return
}
